package com.example.stock.controller;

import com.example.stock.business.WeekLowStaBusiness;
import com.example.stock.controller.dto.WeekPctStatDTO;
import com.example.stock.controller.vo.WeekPctStatVO;
import com.example.stock.mapper.WeekDetailMapper;
import com.example.stock.model.WeekDetail;
import com.example.stock.service.IndustryDataService;
import com.example.stock.service.WeekDataService;
import com.example.stock.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@CrossOrigin(origins = "*")
@RequestMapping("week")
@RestController
public class WeekDataController {

    @Autowired
    WeekDataService weekDataService;

    @Autowired
    IndustryDataService industryDataService;

    @GetMapping("{stock}")
    Result index(@PathVariable("stock") String stock) {
        return ResultGenerator.genSuccessResult(weekDataService.getWeekData(stock));
    }

    @GetMapping("meanGt")
    Result meanGt(@RequestParam(required = false) String date) {
        List<String> weeks = weekDataService.getWeekPctStatKeys();

        List<WeekPctStatDTO> list;
        if (date == null) {
            list = weekDataService.getWeekMeanGt(weeks.getFirst());
        } else {
            list = weekDataService.getWeekMeanGt(date);
        }

        Map<String, Object> ret = new HashMap<>();
        ret.put("date", weeks);
        ret.put("data", list);
        return ResultGenerator.genSuccessResult(ret);
    }

    // 周跌幅列表
    @GetMapping("weekPctStat")
    public Result getWeekPctStat() {
        WeekPctStatVO weekPctStatVO = new WeekPctStatVO();

        List<String> weeks = weekDataService.getWeekPctStatKeys(150);

        weekPctStatVO.setWeeks(weeks);
        weekPctStatVO.setData(weekDataService.getWeekPctStatList(weeks.getFirst()));

        return ResultGenerator.genSuccessResult(weekPctStatVO);
    }

    @GetMapping("weekPctStat/{week}")
    public Result getWeekPctStatByWeek(@PathVariable String week) {

        WeekPctStatVO weekPctStatVO = new WeekPctStatVO();

        List<String> weeks = weekDataService.getWeekPctStatKeys(150);
        weekPctStatVO.setWeeks(weeks);
        if (!weeks.contains(week)) {
            return ResultGenerator.genSuccessResult(weekPctStatVO);
        }

        weekPctStatVO.setData(weekDataService.getWeekPctStatList(week));

        return ResultGenerator.genSuccessResult(weekPctStatVO);
    }

    @GetMapping("industry/{industry}")
    public Result industry(@PathVariable("industry") String industry) {
        return ResultGenerator.genSuccessResult(format(industryDataService.getStocksByTag(industry), 200));
    }

    @GetMapping("multiStock")
    public Result multiStock(@RequestParam("stocks") String stocks, @RequestParam(defaultValue = "200") int count) {
        String[] stockSplit = stocks.split(DataUtil.STOCK_SPLIT);
        return ResultGenerator.genSuccessResult(format(List.of(stockSplit), count));
    }

    private Map<String, List<WeekDetail>> format(List<String> stocks, int count) {
        Map<String, List<WeekDetail>> weekRawData = weekDataService.getWeekRawData(stocks);
        // 限制返回个数
        Map<String, List<WeekDetail>> ret = new LinkedHashMap<>();
        stocks.forEach(stock -> {
            ret.put(stock,
                    weekRawData.get(stock).stream()
                            .sorted(Comparator.comparing(WeekDetail::getDate).reversed())
                            .limit(count).toList()
            );
        });

        return ret;
    }

    @Autowired
    WeekLowStaBusiness weekLowStaBusiness;

    @GetMapping("lowSta")
    Result lowSta() {
        return ResultGenerator.genSuccessResult(weekLowStaBusiness.getWeekLowSta());
    }

    @GetMapping("mostDe")
    Result decrease() {
        return ResultGenerator.genSuccessResult(weekDataService.getWeekPctMostDecrease(300));
    }

    @GetMapping("mostPctSum")
    Result pctSum() {
        return ResultGenerator.genSuccessResult(weekDataService.getWeekMostPctSum());
    }

    @Autowired
    WeekDetailMapper weekDetailMapper;

    @GetMapping("consecutive/{stock}")
    Result consecutive(@PathVariable String stock) {
        return ResultGenerator.genSuccessResult(weekDetailMapper.getConsecutiveByStock(stock));
    }
}